package ForSoSo.entity;

import ForSoSo.common.Common;
import ForSoSo.service.CallService;
import ForSoSo.service.NetService;
import ForSoSo.service.SendService;

public class SuperPackage extends ServicePackage implements CallService, SendService, NetService {
    private int talkTime;//通话时长(分钟)
    private int smsCount;//短信条数
    private int flow;//上网流量(MB)

    public SuperPackage() {
        //初始化套餐数据
        this.talkTime = 200;
        this.smsCount = 50;
        this.flow = 1 * 1024;
        this.price = 78.0;
    }

    public SuperPackage(int talkTime, int smsCount, int flow) {
        this.talkTime = talkTime;
        this.smsCount = smsCount;
        this.flow = flow;
    }

    @Override
    public void showInfo() {
        System.out.println("超人套餐：通话时长为：" + this.talkTime + "分钟/月,短信条数为：" +
                this.smsCount + "条/月,上网流量为" + this.flow / 1024 + "GB/月");
    }

    /*
     * 提供通话服务
     * */
    @Override
    public int call(int minCount, MobileCard card) throws Exception {
        int temp = minCount;
        for (int i = 0; i < minCount; i++) {
            if (this.talkTime - card.getRealTalkTime() >= 1) {
                //第一种情况：套餐剩余通话时长可以付1分钟通话
                card.setRealTalkTime(card.getRealTalkTime() + 1);//实际通话数据加1
            } else if (card.getMoney() >= 0.2) {
                //第二种情况：套餐通话时长已用完，账户余额可以只复1分钟通话，使用账户余额只复
                card.setRealTalkTime(card.getRealTalkTime() + 1);//事记使用通话时长1分钟
                card.setMoney(Common.sub(card.getMoney(), 0.2));//账户余额消费0.2元,(1分钟额外通话)
                card.setConsumAmount(card.getConsumAmount());
            } else {
                temp = i;//纪律实际通话分钟数
                throw new Exception("本次已通话" + i + "分钟，您的余额不足，请充值后再使用！");
            }
        }
        return temp;
    }

    /*
     * 提供上网功能
     * */
    @Override
    public int netPlay(int flow, MobileCard card) throws Exception {
        int temp = flow;
        for (int i = 0; i < flow; i++) {
            if (this.flow - card.getRealFlow() >= 1) {
                //第一种情况：套餐剩余流量可以支持使用1M流量
                card.setRealFlow(card.getRealFlow() + 1);  //实际使用流量+1MB
            } else if (card.getMoney() >= 0.1) {
                //第二种情况：套餐流量已用完，账户余额可以只复1M流量,使用账户余额支付
                card.setRealFlow(card.getRealFlow() + 1);//实际使用流量+1MB
                card.setMoney(Common.sub(card.getMoney(), 0.1));//账户余额消费0.1元(1M流量费用)
                card.setConsumAmount(card.getConsumAmount() + 0.1);
            } else {
                temp = i;
                throw new Exception("本次已使用流量" + i + "您的余额不足，请充值后再使用！");
            }
        }
        return temp;
    }

    @Override
    public int sendMessage(int count, MobileCard card) throws Exception {
        int temp = smsCount;
        for (int i = 0; i < smsCount; i++) {
            if (this.smsCount - card.getRealSMSCount() >= 1) {
                //第一种情况：套餐剩余流量可以支持使用1M流量
                card.setRealSMSCount(card.getRealSMSCount() + 1);//实际使用短信条数加1
            }else if (card.getMoney()>=0.1){
                //第二种情况：套餐短信已用完，账户余额可以支付1条短信,使用账户余额支付
                card.setRealFlow(card.getRealFlow() + 1);//
                card.setMoney(Common.sub(card.getMoney(), 0.1));//账户余额消费0.1元(1条短信费用)
                card.setConsumAmount(card.getConsumAmount() + 0.1);
            }else {
                temp = i;
                throw new Exception("本次已使用流量" + i + "您的余额不足，请充值后再使用！");
            }
        }
        return temp;
    }

    /**
     * 获取
     *
     * @return talkTime
     */
    public int getTalkTime() {
        return talkTime;
    }

    /**
     * 设置
     *
     * @param talkTime
     */
    public void setTalkTime(int talkTime) {
        this.talkTime = talkTime;
    }

    /**
     * 获取
     *
     * @return smsCount
     */
    public int getSmsCount() {
        return smsCount;
    }

    /**
     * 设置
     *
     * @param smsCount
     */
    public void setSmsCount(int smsCount) {
        this.smsCount = smsCount;
    }

    /**
     * 获取
     *
     * @return flow
     */
    public int getFlow() {
        return flow;
    }

    /**
     * 设置
     *
     * @param flow
     */
    public void setFlow(int flow) {
        this.flow = flow;
    }

    public String toString() {
        return "SuperPackage{talkTime = " + talkTime + ", smsCount = " + smsCount + ", flow = " + flow + "}";
    }
}
